<template>
	<div class="login">
		<router-link :to="{name:'Home'}"><img class="logo" src="../assets/images/logo.jpg" alt=""></router-link>
		<van-form @submit="onSubmit">
			<van-field v-model="userName" name="userName" label="用户名" placeholder="用户名" :rules="[{ required: true, message: '请填写用户名' }]" />
			<van-field v-model="password" type="password" name="password" label="密码" placeholder="密码" :rules="[{ required: true, message: '请填写密码' }]" />
			<div style="margin: 16px;"><van-button round block type="info" native-type="submit">登录</van-button></div>
		</van-form>
		<router-link style="float:right;margin:16px" :to="{name:'Reg'}">没有账号,我要注册</router-link>
	</div>
</template>

<script>
import {Toast} from "vant"
import {loginAPI} from "../services/auth"
import {setToken} from "../utils/tools"
export default {
	name: 'Login',
	data() {
		return {
			userName: '',
			password: ''
		};
	},
	methods: {
		async onSubmit(values) {
			const resLogin=await loginAPI(values);
			if(resLogin.code===1){
				setToken(resLogin.data)
				Toast.success("登录成功")
				this.$router.push({name:'User'}) 
				//登录成功获取购车数量
				this.$eventBus.$emit("updateCartCount");
			}else{
				Toast.fail(resLogin.data)
			}
		}
	}
};
</script>

<style scoped="scoped" lang="scss">
</style>
